package com.feri.fc.ocr;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import com.feri.fc.config.RedisKeyConfig;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/26 11:42
 * @Modified:
 */
public class BaiduOcrUtil {
    public static final String KEY="wKE4Iuyd383Wld3GXCKCGNF7";
    public static final String SECRET="cUDtMt9n5Ur4xoQRKiLizW5flGLS15AZ";


    /**
     * 获取令牌
     * 有效期 30天*/
    public static String authToken(){
        String url="https://aip.baidubce.com/oauth/2.0/token";
        //1.创建请求
        HttpRequest request=new HttpRequest(url).method(Method.POST);
        //2.准备参数
        //grant_type： 必须参数，固定为client_credentials；
        //client_id： 必须参数，应用的API Key；
        //client_secret： 必须参数，应用的Secret Key；
        StringBuilder param=new StringBuilder();
        param.append("grant_type=client_credentials&");
        param.append("client_id="+KEY+"&");
        param.append("client_secret="+SECRET);
        request.body(param.toString());

        //3.发起请求 获取响应
        HttpResponse response= request.execute();
        if(response.isOk()){
            return response.body();
        }
        return null;
    }
    /**
     * ocr识别身份证
     * type取值：
     * -front：身份证含照片的一面
     * -back：身份证带国徽的一面*/
    public static String ocrIdCard(String token,String card,String type){
        String url="https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";

        HttpRequest request=new HttpRequest(url+"?access_token="+token).method(Method.POST);
        request.header("Content-Type","application/x-www-form-urlencoded");
        StringBuilder param=new StringBuilder();
        param.append("url="+card+"&");
        param.append("id_card_side="+type);
        request.body(param.toString());

        HttpResponse response= request.execute();
        if(response.isOk()){
            return response.body();
        }
        return null;
    }
    /**
     * 识别身份证正面 并解析结果*/
    public static IdCard ocrIdCardFront(String token,String card){
       String result=ocrIdCard(token,card,"front");
       if(StringUtils.hasLength(result)){
           System.err.println(result);
           JSONObject jo=new JSONObject(result);
           if(jo.getStr("image_status").equals("normal")){
               JSONObject jo2=jo.getJSONObject("words_result");
               return new IdCard(jo2.getJSONObject("姓名").getStr("words"),
                       jo2.getJSONObject("住址").getStr("words"),
                       jo2.getJSONObject("性别").getStr("words"),
                       jo2.getJSONObject("公民身份号码").getStr("words"));
           }
       }
        return null;
    }
    /**
     * 通用识别图片内容*/
    public static String ocrImg(String token,String img){
        String url="https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";


        HttpRequest request=new HttpRequest(url+"?access_token="+token).method(Method.POST);
        request.header("Content-Type","application/x-www-form-urlencoded");
        StringBuilder param=new StringBuilder();
        param.append("url="+img);
        request.body(param.toString());

        HttpResponse response= request.execute();
        if(response.isOk()){
            return response.body();
        }
        return null;
    }
}
